package com.yiyukongjian.house.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiyukongjian.house.R;

public class TopNavigator extends LinearLayout {
	
	private TextView mMidTv = null;
	private TextView mRightTv = null;
	private TextView mLeftTv = null;
	public TopNavigator(Context context) {
		super(context);
	}

	public TopNavigator(Context context, AttributeSet attrs) {		
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.topnavigator, this, true);
		setWeightSum(1.0f);
		mMidTv = (TextView) findViewById(R.id.navigator_midtv);
		mRightTv = (TextView) findViewById(R.id.navigator_right);
		mLeftTv = (TextView) findViewById(R.id.navigator_lefttv);
				
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopNavigatorWidget);

		final int cnt = array.getIndexCount();
		for(int i = 0; i < cnt; i++){
			int attr = array.getIndex(i);
			int resouceId = -1;
			switch(attr){
			case R.styleable.TopNavigatorWidget_righttext:
				resouceId = array.getResourceId(R.styleable.TopNavigatorWidget_righttext,0);
				mRightTv.setText(resouceId > 0 ? array.getResources().getText(resouceId) : array.getString(R.styleable.TopNavigatorWidget_righttext));
				break;
			case R.styleable.TopNavigatorWidget_midtext:
				resouceId = array.getResourceId(R.styleable.TopNavigatorWidget_midtext,0);
				mMidTv.setText(resouceId > 0 ? array.getResources().getText(resouceId) : array.getString(R.styleable.TopNavigatorWidget_midtext));				
				break;
			case R.styleable.TopNavigatorWidget_lefttext:
				resouceId = array.getResourceId(R.styleable.TopNavigatorWidget_lefttext,0);
				mLeftTv.setText(resouceId > 0 ? array.getResources().getText(resouceId) : array.getString(R.styleable.TopNavigatorWidget_lefttext));				
				break;
		    default:
		    	break;
			}
		}
				
		array.recycle();
	}
	
	/**
	 * Provide the lister event to the right button of this navigator
	 * @param listener
	 */
	public void addListernOnRightComponent(View.OnClickListener listener){
		if(this.mRightTv != null){
			this.mRightTv.setOnClickListener(listener);
		}
	}
	
	/**
	 * Provide the lister event to the right button of this navigator
	 * @param listener
	 */
	public void addListernOnLeftComponent(View.OnClickListener listener){
		if(this.mLeftTv != null){
			System.out.println("add listener...");
			this.mLeftTv.setOnClickListener(listener);
		}
	}
}
